//
//  HWInviteAlertView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/2.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWInviteAlertView: MMPopupView {

    private var tapBgView:UIView!
    private var bgView: UIView!
    var contentLabel:UILabel!
    var cancelButton:UIButton!
    
    @objc var close:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension HWInviteAlertView {
    private func initUI() -> Void {
        type = MMPopupType.alert
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 18*widthScale
        addSubview(bgView)
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setImage(UIImage(named: "alertCloseButton"), for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonClick(_:)), for: .touchUpInside)
        addSubview(cancelButton)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13*widthScale)
        bgView.addSubview(contentLabel)
        
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.centerY.equalToSuperview()
        }
        
        cancelButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(bgView.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(34*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(24*widthScale)
            make.bottom.equalTo(-24*widthScale)
        }
    }
}
extension HWInviteAlertView {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        hide()
    }
    
    @objc func cancelButtonClick(_ button:UIButton) -> Void {
        hide()
    }
    
}
